package com.anpy.management.service.impl;

import com.anpy.management.entity.TaskEntity;
import com.anpy.management.mapper.TaskMapper;
import com.anpy.management.service.TaskService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 任务服务实现类
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, TaskEntity> implements TaskService {

    @Override
    public boolean createTask(TaskEntity taskEntity) {
        Date now = new Date();
        taskEntity.setCreatedAt(now);
        taskEntity.setUpdatedAt(now);
        
        // 如果进度为空，默认设置为0
        if (taskEntity.getProgress() == null) {
            taskEntity.setProgress(0);
        }
        
        // 如果状态为空，默认设置为"未开始"
        if (StringUtils.isEmpty(taskEntity.getCurrentStatus())) {
            taskEntity.setCurrentStatus("NOT_STARTED");
        }
        taskEntity.setTaskId(null);
        return save(taskEntity);
    }

    @Override
    public boolean updateTask(TaskEntity taskEntity) {
        taskEntity.setUpdatedAt(new Date());
        return updateById(taskEntity);
    }

    @Override
    public boolean deleteTask(Integer taskId) {
        return removeById(taskId);
    }

    @Override
    public TaskEntity getTask(Integer taskId) {
        return getById(taskId);
    }

    @Override
    public List<TaskEntity> getUserTasks(Long userId) {
        LambdaQueryWrapper<TaskEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskEntity::getUserId, userId);
        return list(queryWrapper);
    }

    @Override
    public List<TaskEntity> getProjectTasks(Integer projectId) {
        LambdaQueryWrapper<TaskEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskEntity::getProjectId, projectId);
        return list(queryWrapper);
    }

    @Override
    public Page<TaskEntity> getUserTasksByPage(int page, int size, Long userId, 
                                              Integer projectId, String currentStatus, 
                                              String taskName) {
        Page<TaskEntity> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<TaskEntity> queryWrapper = Wrappers.lambdaQuery();
        
        // 必须条件：用户ID
        queryWrapper.eq(TaskEntity::getUserId, userId);
        
        // 可选条件：项目ID
        if (projectId != null) {
            queryWrapper.eq(TaskEntity::getProjectId, projectId);
        }
        
        // 可选条件：当前状态
        if (!StringUtils.isEmpty(currentStatus)) {
            queryWrapper.eq(TaskEntity::getCurrentStatus, currentStatus);
        }
        
        // 可选条件：任务名称（模糊查询）
        if (!StringUtils.isEmpty(taskName)) {
            queryWrapper.like(TaskEntity::getTaskName, taskName);
        }
        
        // 按创建时间降序排序
        queryWrapper.orderByDesc(TaskEntity::getEndTime);
        
        return page(pageParam, queryWrapper);
    }
}
